<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('风控编号')" prop="riskControlNumber">
            <el-input v-model="form.riskControlNumber" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('适用产品')" prop="applicableProducts">
            <el-input v-model="form.applicableProducts" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('规则类型')" prop="ruleType">
            <el-select v-model="form.ruleType" :placeholder="$t('规则类型')" clearable>
              <el-option
                v-for="item in ruleTypeOption"
                :key="item.dictValue"
                :value="item.dictValue"
                :label="item.dictLabel"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('规则状态')" prop="ruleStatus">
            <el-select v-model="form.ruleStatus" :placeholder="$t('规则状态')" clearable>
              <el-option :value="'KQ'" :label="$t('开启')" />
              <el-option :value="'TY'" :label="$t('停用')" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('备注')" prop="riskControlDescription">
            <el-input v-model="form.riskControlDescription" type="textarea" :placeholder="$t('备注')" show-word-limit :maxlength="2000" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <el-button @click="cancel()">{{ $t("取消") }}</el-button>
      <el-button type="primary" @click="submitForm()">{{ $t("保存") }}</el-button>
    </div>
  </div>
</template>

<script>

export default {
  data() {
    return {
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        riskControlNumber: { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] },
        ruleType: { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
      },
      ruleTypeOption: [
        { dictValue: '0', dictLabel: '久期控制' },
        { dictValue: '1', dictLabel: '投资标的控制' },
        { dictValue: '2', dictLabel: '杠杆率控制' },
        { dictValue: '3', dictLabel: '成本控制' }
      ],
      // 遮罩层
      loading: false
    }
  },
  created() {
    this.$bus.$emit('async_done')
  },
  methods: {
    // 初始化数据
    init(data) {
      this.loading = true
      if (data != null) {
        setTimeout(() => {
        this.form = data
        this.loading = false
        }, 200)
      } else {
        this.reset()
        this.loading = false
      }
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        riskControlNumber: undefined,
        ruleType: undefined,
        riskControlDescription: undefined,
        applicableProducts: undefined,
        lastModified: undefined,
        ruleStatus: undefined,
        fileList: []
      }
      this.resetForm('form')
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
    },
    // 关闭form页面
    closeForm() {
      // 关闭form页面遮罩层
      this.loading = false
      // 关闭form页面
      if (this.$parent.$parent.formOpen !== undefined) {
        this.$parent.$parent.formOpen = false
      }
      // 刷新list页面
      if (this.$parent.$parent.getList !== undefined) {
        this.$parent.$parent.getList()
      }
    },
    /** 提交按钮 */
    submitForm: function() {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate(valid => {
          // 验证数据
          if (valid) {
            // 弹出遮罩层
            this.loading = true
            this.msgSuccess(this.$t('操作成功'))
            this.form.lastModified = new Date()
            this.closeForm()
          } else {
            reject()
            this.locationError(document)
          }
        })
      })
    }
  }
}
</script>
